package com.contactbook.web.controller.contact;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.contactbook.web.service.ContactClient;
import com.contactbook.web.service.GroupClient;
import com.contactbook.web.util.ContactMessage;

import contactbook.model.Account;
import contactbook.model.Contact;
import contactbook.model.MaritalStatus;

public class AddContactController extends HttpServlet implements Servlet {
	
	private static final long serialVersionUID = 1L;
	
	private static final String CONFIRM_ADD = "Ajouter";
	private static final String CANCEL_ADD = "Annuler";
	
	private GroupClient groupClient;
	private ContactClient contactClient;
	
	public AddContactController() {
		super();
		groupClient = new GroupClient();
		contactClient = new ContactClient();
	}
	
	public void init() throws ServletException {
    	super.init();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Account account = (Account)session.getAttribute("account");
		String submit = request.getParameter("submit");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");;
		
		if (account != null) {
			groupClient.login(account.getLogin(), account.getPassword());
			contactClient.login(account.getLogin(), account.getPassword());
			
			if (submit == null) {
				request.setAttribute("groups", groupClient.getGroups().iterator());
				request.setAttribute("maritalStatus", MaritalStatus.values());
				dispatcher = request.getRequestDispatcher("/addContact.jsp");
			}
			else if (submit.equals(CONFIRM_ADD)) {
				ContactMessage message = addContact(request);
				request.setAttribute("message", message);
				request.setAttribute("groups", groupClient.getGroups().iterator());
				request.setAttribute("maritalStatus", MaritalStatus.values());
				dispatcher = request.getRequestDispatcher("/addContact.jsp");
			}
			else if (submit.equals(CANCEL_ADD)) {
    			dispatcher = request.getRequestDispatcher("/Contact");
			}

			contactClient.logout();
			groupClient.logout();
		}
		dispatcher.forward(request, response);
	}
	
	private ContactMessage addContact(HttpServletRequest request) {
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		int groupId = Integer.valueOf(request.getParameter("group"));
		String email = request.getParameter("email");
		String phoneNumber = request.getParameter("phoneNumber");
		String address = request.getParameter("address");
		String stringZipCode = request.getParameter("zipCode");
		int zipCode = (stringZipCode.equals("")) ? 0 : Integer.valueOf(stringZipCode);
		String town = request.getParameter("town");
		String country = request.getParameter("country");
		MaritalStatus maritalStatus = MaritalStatus.valueOf(request.getParameter("maritalStatus"));
		String stringAge = request.getParameter("age");
		int age = (stringAge.equals("")) ? 0 : Integer.valueOf(stringAge);
		
		if (!lastName.equals("")) {
			Contact contact;
			if (groupId == 0)
				contact = contactClient.add(new Contact(firstName, lastName));
			else
				contact = contactClient.add(new Contact(firstName, lastName), groupId);
			if (contact != null) {
				contact.setEmail(email);
				contact.setPhoneNumber(phoneNumber);
				contact.setAddress(address);
				contact.setZipCode(zipCode);
				contact.setTown(town);
				contact.setCountry(country);
				contact.setMaritalStatus(maritalStatus);
				contact.setAge(age);
				contactClient.update(contact);
				return ContactMessage.CONTACT_CREATED_OK;
			}
			else
				return ContactMessage.CONTACT_CREATED_FAIL;
		}
		return ContactMessage.CONTACT_CREATED_LASTNAME_FAIL;
	}
	
	public void destroy() {
		super.destroy();
	}

}
